<?php
//checkoutnow.php
session_start();
include_once("config/config.inc.php");

if($_POST["act"]=="checkoutnow") {
	$room = $_POST["room"];
	$sql = " select a.occupancy_id, a.actual_checkin, b.door_name, c.rate_name, a.shift_checkin, d.fullname
			from occupancy a, rooms b, rates c, users d
			where a.room_id=b.room_id and a.rate_id=c.rate_id and a.update_by=d.user_id and a.room_id=$room and actual_checkout='0000-00-00 00:00:00' ";
	$res = mysql_query($sql);

	if(mysql_num_rows($res) > 0) {
		list($occupancy, $checkindate,$doorname,$ratename,$shift,$fullname)=mysql_fetch_row($res);
		
		//update occupancy
		$now = date("Y-m-d H:i:s");
		$sql = "update occupancy set actual_checkout='$now', update_by='$user' 
				where occupancy_id='$occupancy'";
		mysql_query($sql);
		
		//add occupancy log
		$sql = " insert into occupancy_log(transaction_date, occupancy_id, update_by, remarks ) value ('$now', '$occupancy', '$user', 'CheckOut' ) ";
		mysql_query($sql) or die($sql);
		
		//update room status
		$sql = " update rooms set status=3, last_update='$now',update_by='$user' where room_id='$room' ";
		mysql_query($sql) or die($sql);
		
		//insert payment
		//payments
		if($cash) {
			$sql = " insert into salesreceipts(receipt_date, occupancy_id, tendertype, amount) 
				values ('$now', '$newoccupancy', 'Cash', '$cash')";
			mysql_query($sql);
		}

		if($card) {
			$sql = " insert into salesreceipts(receipt_date, occupancy_id, tendertype, amount) 
				values ('$now', '$newoccupancy', 'Card', '$card')";
			mysql_query($sql);
			//insert card details
			$newsalesid = mysql_insert_id();
			$sql = " insert into card_payment_details (salesreceipt_id, cardtype, transaction_number, batch_number) 
					values ('$newsalesid','$cardtype','$trxnid','$batch')";
			mysql_query($sql);
		}
	}
}
?>